﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GovernmentOfficeProject.SysTools;
using GovernmentOfficeProject.LevelDataAccessModel;
using GovernmentOfficeProject.LevelBusinessLogic;

namespace GovernmentOfficeProject.LevelViewUI.admin
{
    public partial class UserManager : System.Web.UI.Page
    {
        private static string strDelUserID = "";
        private static List<Entity_USER_User> userList = null;
        private static bool blSelectBtnState = false;   //

        protected void Page_Load(object sender, EventArgs e)
        {
            //权限判断
            if (SystemContext.CurrentUserID == 0 || SystemContext.IsAdmin == 0)
            {
                Response.Redirect("/LevelViewUI/admin/Login.htm?FromUrl=" + HttpUtility.UrlEncode(Request.Url.AbsoluteUri));
            }
            else
            {
                SystemContext.CurrentUserID = SystemContext.CurrentUserID;
                SystemContext.IsAdmin = 1;
            }
            if (!Page.IsPostBack)
            {
                gridDataBind();
                operationFeedbackByLabel("页面加载完毕.");
            }
        }

        //给带条件的btn用.
        private void gridDataBind(object objDataSource) 
        {
            GridViewUserManager.DataSource = (List<Entity_USER_User>)objDataSource;
            GridViewUserManager.DataBind();
        }

        //给btnALL用.
        private void gridDataBind()
        {
            if (userList!=null)
            {
                gridDataBind(userList);
            }
            else
            {
                gridDataBind(new BLL_USER().getAllUser());
            }
        }

        //操作状态显示
        private void operationFeedbackByLabel(string strFeedback)
        {
            lblOperationFeedback.Text = "操作状态: ";
            if (GridViewUserManager.Rows.Count.Equals(0))
            {
                lblOperationFeedback.Text += "没有相关用户.";
            }
            else
            {
                lblOperationFeedback.Text += strFeedback;
            }
        }


        //用户查看,转到查看页面.
        protected void ButtonUserDetail_Click(object sender, EventArgs e)
        {
            //查看用户:查看用户资料并修改
            //操作后可返回UserManager.aspx页面. 
            string strUserID=((Button)sender).CommandArgument.ToString();
            string strURL = "UserManagerDetail.aspx?umdval=" + strUserID + "&umduse=detail";
            Response.Redirect(strURL);
        }

        //用户删除.(删除已禁用.)
        protected void ButtonUserDel_Click(object sender, EventArgs e)
        {
            strDelUserID = ((Button)sender).CommandArgument.ToString();
            decimal dclDelUserID = Convert.ToDecimal(strDelUserID.Trim());
            bool flag = new BLL_USER().deleleUserByUserID(dclDelUserID);            
            //删除后页面提示成功失败.
            if (flag)
            {
                operationFeedbackByLabel("删除用户成功.");
            }
            else
            {
                operationFeedbackByLabel("删除用户失败.");
            }

            //如果txt条件框有值,则删除后重新获取根据条件获得的数据.
            string strLoginName = this.txtUserName.Text.ToString().Trim();
            string strRealName = this.txtRealName.Text.ToString().Trim();
            decimal dclUserStatus = Convert.ToDecimal(this.ddlUserStatus.SelectedValue.ToString().Trim());
            if (blSelectBtnState)
            {
                Entity_USER_User user = new Entity_USER_User();
                if (strLoginName != "" && strLoginName != null)
                {
                    user.LoginName = strLoginName;
                }
                if (strRealName != "" && strLoginName != null)
                {
                    user.RealName = strRealName;
                }
                user.RecordStatus = dclUserStatus;
                gridDataBind(new BLL_USER().getUserListByCondition(user));
                operationFeedbackByLabel("删除用户成功.重新加载数据成功.");
                
            }
            else
            {
                gridDataBind(new BLL_USER().getAllUser());
                operationFeedbackByLabel("删除用户成功,获取数据成功.");
                
            }
        }


        //protected void DDLStateOperation_DataBinding(object sender, EventArgs e)
        //{
        //    DropDownList ddlso = (DropDownList)sender;
        //    if (ddlso.SelectedValue.Equals("启用"))
        //    {
        //        ddlso.BackColor = System.Drawing.Color.FromName("#3399FF");
        //    }
        //    if (ddlso.SelectedValue.Equals("禁用"))
        //    {
        //        ddlso.BackColor = System.Drawing.Color.Red;
        //    }
        //}
        //设置用户状态控件颜色.
        protected void DDLStateOperation_DataBound(object sender, EventArgs e)
        {
            DropDownList ddlso =  (DropDownList)sender;
            if (ddlso.SelectedValue.Equals("启用"))
            {
                ddlso.BackColor = System.Drawing.Color.FromName("#3399FF");
            }
            if (ddlso.SelectedValue.Equals("禁用"))
            {
                ddlso.BackColor = System.Drawing.Color.Red;
            }
        }           

        //修改用户状态(启用\禁用).
        protected void DDLStateOperation_SelectedIndexChanged(object sender, EventArgs e)
        {
            //判断选择
            DropDownList ddlso = (DropDownList)sender;
            string strDDLValue = ddlso.SelectedValue;
            GridViewRow gdrow = (GridViewRow)((DropDownList)sender).Parent.Parent;
            decimal dclUpUID = Convert.ToDecimal(gdrow.Cells[0].Text.ToString().Trim());
            Entity_USER_User userParameter = null;
            if (strDDLValue.Equals("启用"))
            {
                userParameter = new Entity_USER_User();
                userParameter.RecordStatus = 1;
                if (new BLL_USER().updateUserByUserID(dclUpUID,userParameter))
                {                   
                    operationFeedbackByLabel("已更新用户状态--启用.");
                }
            }
            if (strDDLValue.Equals("禁用"))
            {
                userParameter = new Entity_USER_User();
                userParameter.RecordStatus = 0;
                if (new BLL_USER().updateUserByUserID(dclUpUID, userParameter))
                {                    
                    operationFeedbackByLabel("已更新用户状态--禁用.");
                }
            }
            DDLStateOperation_DataBound(sender, e);
        }

        //根据条件获得用户列表.
        protected void btnSelect_Click(object sender, EventArgs e)
        {
            blSelectBtnState = true;
            string strLoginName = this.txtUserName.Text.ToString().Trim();
            string strRealName = this.txtRealName.Text.ToString().Trim();
            decimal dclUserStatus = Convert.ToDecimal(this.ddlUserStatus.SelectedValue.ToString().Trim());
            Entity_USER_User user = new Entity_USER_User();
            if (strLoginName != "" && strLoginName != null)
            {
                blSelectBtnState = true;
                user.LoginName = strLoginName;
            }
            if (strRealName != "" && strLoginName != null)
            {
                user.RealName = strRealName;
                blSelectBtnState = true;
            }
            if (dclUserStatus==0 || dclUserStatus==1)
            {
                user.RecordStatus = dclUserStatus;
            }
            gridDataBind(new BLL_USER().getUserListByCondition(user));
            operationFeedbackByLabel("加载数据成功.");
        }

        //获取所有用户.
        protected void btnAllUser_Click(object sender, EventArgs e)
        {
            blSelectBtnState = false;
            gridDataBind(new BLL_USER().getAllUser());
            operationFeedbackByLabel("数据加载成功.已加载所有用户.");
        }

        //添加用户.
        protected void btnAddUser_Click(object sender, EventArgs e)
        {
            string strURL = "UserManagerDetail.aspx?umduse=new"; //0为新增用户的标识参数.
            Response.Redirect(strURL);
        }

        /// <summary>
        /// 重写GridView的PageIndexChanging事件,让分页正常.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// 
        protected void GridViewUserManager_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {            
            
            string strLoginName = this.txtUserName.Text.ToString().Trim();
            string strRealName = this.txtRealName.Text.ToString().Trim();
            decimal dclUserStatus = Convert.ToDecimal(this.ddlUserStatus.SelectedValue.ToString().Trim());
            if (blSelectBtnState)
            {
                Entity_USER_User user = new Entity_USER_User();
                if (strLoginName != "" && strLoginName != null)
                {
                    user.LoginName = strLoginName;
                }
                if (strRealName != "" && strLoginName != null)
                {
                    user.RealName = strRealName;
                }
                user.RecordStatus = dclUserStatus;
                GridViewUserManager.PageIndex = e.NewPageIndex;                
                gridDataBind(new BLL_USER().getUserListByCondition(user));
                operationFeedbackByLabel("索引第" + (GridViewUserManager.PageIndex + 1).ToString() + "页.");
                
            }
            else
            {
                GridViewUserManager.PageIndex = e.NewPageIndex;
                gridDataBind(new BLL_USER().getAllUser());
                operationFeedbackByLabel("索引第" + (GridViewUserManager.PageIndex+1).ToString() + "页.");

            }
        }

        protected void GridViewUserManager_PageIndexChanged(object sender, EventArgs e)
        {
            string strLoginName = this.txtUserName.Text.ToString().Trim();
            string strRealName = this.txtRealName.Text.ToString().Trim();
            decimal dclUserStatus = Convert.ToDecimal(this.ddlUserStatus.SelectedValue.ToString().Trim());
            if (blSelectBtnState)
            {
                Entity_USER_User user = new Entity_USER_User();
                if (strLoginName != "" && strLoginName != null)
                {
                    user.LoginName = strLoginName;
                }
                if (strRealName != "" && strLoginName != null)
                {
                    user.RealName = strRealName;
                }
                user.RecordStatus = dclUserStatus;
  
                gridDataBind(new BLL_USER().getUserListByCondition(user));
                operationFeedbackByLabel("索引第" + (GridViewUserManager.PageIndex + 1).ToString() + "页.");
                blSelectBtnState = true;
            }
            else
            {
                gridDataBind(new BLL_USER().getAllUser());
                operationFeedbackByLabel("索引第" + (GridViewUserManager.PageIndex + 1).ToString() + "页.");
                
            }
        }

        protected void GridViewUserManager_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string strLoginName = this.txtUserName.Text.ToString().Trim();
            string strRealName = this.txtRealName.Text.ToString().Trim();
            decimal dclUserStatus = Convert.ToDecimal(this.ddlUserStatus.SelectedValue.ToString().Trim());
            if (blSelectBtnState)
            {
                Entity_USER_User user = new Entity_USER_User();
                if (strLoginName != "" && strLoginName != null)
                {
                    user.LoginName = strLoginName;
                }
                if (strRealName != "" && strLoginName != null)
                {
                    user.RealName = strRealName;
                }
                user.RecordStatus = dclUserStatus;
                gridDataBind(new BLL_USER().getUserListByCondition(user));
                operationFeedbackByLabel("删除成功,重新加载数据成功.");                
            }
            else
            {
                gridDataBind(new BLL_USER().getAllUser());
                operationFeedbackByLabel("已获得所有用户.");                
            }
        }

        protected void ddlUserStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            blSelectBtnState = true;
        }

        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridViewUserManager.PageSize = Convert.ToInt32(this.ddlPageSize.SelectedValue.ToString());
            string strLoginName = this.txtUserName.Text.ToString().Trim();
            string strRealName = this.txtRealName.Text.ToString().Trim();
            decimal dclUserStatus = Convert.ToDecimal(this.ddlUserStatus.SelectedValue.ToString().Trim());
            if (blSelectBtnState)
            {
                Entity_USER_User user = new Entity_USER_User();
                if (strLoginName != "" && strLoginName != null)
                {
                    user.LoginName = strLoginName;
                }
                if (strRealName != "" && strLoginName != null)
                {
                    user.RealName = strRealName;
                }
                user.RecordStatus = dclUserStatus;
                gridDataBind(new BLL_USER().getUserListByCondition(user));
                operationFeedbackByLabel("已设置每页显示" + GridViewUserManager.PageSize.ToString() + "个用户.");
            }
            else
            {
                gridDataBind(new BLL_USER().getAllUser());
                operationFeedbackByLabel("已设置每页显示" + GridViewUserManager.PageSize.ToString() + "个用户.");
            }
        }


        
    }
}
